package com.lanou.connotationjoke.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Toast;

import com.lanou.connotationjoke.view.NewListView;
import com.lanou.connotationjoke.R;
import com.lanou.connotationjoke.activity.WebViewActivity;
import com.lanou.connotationjoke.adapter.HomeSubscriptionListViewAdapter;
import com.lanou.connotationjoke.db.HomeSubscriptionEntity;
import com.lanou.connotationjoke.db.HomeSubscriptionEntityDao;
import com.lanou.connotationjoke.tool.DaoSingleton;
import com.lanou.connotationjoke.tool.MyAppllication;

import java.util.List;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import de.greenrobot.event.ThreadMode;

/**
 * Created by dllo on 16/1/8.
 */
public class HomeSubscriptionFragment extends BaseFragment{
    private NewListView listView;
    private HomeSubscriptionListViewAdapter adapter;
    private List<HomeSubscriptionEntity> datas;
    private HomeSubscriptionEntityDao entityDao =
            DaoSingleton.getInstance().getHomeSubscriptionEntityDao();
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home_subscription,null);
        listView = (NewListView) view.findViewById(R.id.listview_home_subscription);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

            EventBus.getDefault().register(this);



        datas = entityDao.queryBuilder().list();
        adapter = new HomeSubscriptionListViewAdapter(getActivity(),datas);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (MyAppllication.hasNetwork()) {
                    WebViewActivity.goToWebViewActivity(getActivity(), datas.get(position).getShare_url());
                } else {
                    Toast.makeText(getActivity(), "无网络", Toast.LENGTH_SHORT).show();
                }
            }
        });
        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                showDialog(position);
                return true;
            }
        });
    }
//在fragment生命周期中Eventbus取消注册需要在onstop()方法中进行
    //滑动页面时退出fragment会执行onstop()方法取消注册,不会执行ondestory()方法
    //这样避免重复这侧导致程序崩溃
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MainThread)
    public void onMainEvent(String s) {
        if ("refresh".equals(s)) {
            datas = entityDao.queryBuilder().list();
            adapter.refreshData(datas);
        }
    }
    private void showDialog(final int pos){
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("确定要取消该订阅吗");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                HomeSubscriptionEntity homeSubscriptionEntity = datas.get(pos);
                entityDao.delete(homeSubscriptionEntity);
                datas.remove(pos);
                adapter.refreshData(datas);
            }
        });
        builder.setNegativeButton("取消", null);
        builder.show();
    }
}
